﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using V5_Apps.Core;
using V5_Apps.Plugins.TorrentSearch.Core;
using WeifenLuo.WinFormsUI.Docking;

namespace V5_Apps.Plugins.TorrentSearch {
    public partial class frmMain : DockContent, IApp {
        public frmMain() {
            InitializeComponent();
        }

        public ToolStripMenuItem[] GetMenuItem() {
            var list = new List<ToolStripMenuItem>();
            var ts1 = new ToolStripMenuItem() { Text = "工具1" };
            ts1.Click += (sender, e) => MessageBox.Show(@"工具1");
            list.Add(ts1);

            var t2 = new ToolStripMenuItem() { Text = @"工具2" };
            t2.Click += delegate(object sender, EventArgs e) {
                MessageBox.Show("工具2");
            };
            list.Add(t2);

            return list.ToArray();
        }


        public ToolStripItem[] GetToolBarItem() {
            throw new NotImplementedException();
        }

        public ToolStripItem[] GetStatusItem() {
            throw new NotImplementedException();
        }
        TaskHelper<string> task = new TaskHelper<string>();
        private void btnStart_Click(object sender, EventArgs e) {
            task.ThreadCount = 10;
            task.OutTaskDataWorkDelegate = delegate(Queue<string> queue) {
                int loopNum = 0;
                while (true) {
                    if (loopNum >= 1000) {
                        break; ;
                    }
                    queue.Enqueue("数字:" + loopNum);
                    loopNum++;
                }
            };
            task.OutTaskDoWorkDelegate = delegate(string obj) {
                var s = "";
                OutMessage(obj);
            };
            task.Start();
        }

        private void btnStop_Click(object sender, EventArgs e) {
            task.Stop();
        }

        private void OutMessage(string msg) {
            if (this.txtLogView.InvokeRequired) {
                this.txtLogView.Invoke(new MethodInvoker(() => {
                    this.txtLogView.AppendText(msg);
                    this.txtLogView.AppendText("\r\n");
                }));
            }
            else {
                this.txtLogView.AppendText(msg);
                this.txtLogView.AppendText("\r\n");
            }
        }

        private void btnSearch_Click(object sender, EventArgs e) {
            var f = new frmSearchList();
            f.KeyName = this.txtSearchKey.Text;
            f.ShowDialog(this);
        }
    }
}
